package com.china08.yxyapi.api.manage.tec.teaching.zujuan.editcomponent;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 教育局平台办公 编辑试卷
 * 
 */
//@Api(tags = { "v52", "编辑试卷" })
@CrossOrigin
@RestController
public class EditComponentController extends YxyApiBaseController{
	
	@Autowired
	private ElasticQuesService elasticQuesService;
	
	

//	@ApiOperation(value = "编辑试卷")
//	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/editcomponent/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
		List<QuesModel> list =  reqModel.list;
		//便利QuesModel实体类中试题的id
		List<String> quesIdList = list.stream().map(x -> x.quesId)
                .collect(Collectors.toList());
		
		
		
		//set用来给知识点id去重
		Set<Integer> knowIds=new HashSet<Integer>();
		//便利的QuesModel实体类中知识点的id
		for (QuesModel quesModel : list) {
				knowIds.addAll(quesModel.know);
		}
		
		//通过便利试题的id查询到对应的试题
		List<ElasticYxyYxyQuesResource> findByQuesIdIn = elasticQuesService.findByQuesIdIn(quesIdList);
		
		//单选题list
		List<QuesRespModel> danxuan=new ArrayList<QuesRespModel>();
		//多选题list
		List<QuesRespModel> duoxuan=new ArrayList<QuesRespModel>();
		//填空题list
		List<QuesRespModel> tiankong=new ArrayList<QuesRespModel>();
		//解答题list
		List<QuesRespModel> jieda=new ArrayList<QuesRespModel>();
		// 1单选 2填空 8多选 其他解答
		for (ElasticYxyYxyQuesResource es : findByQuesIdIn) {
			String quesType = es.getQuestionType();
			// 单选题
			if (StringUtils.equals("1", quesType)){
				QuesRespModel quesRespModel=new QuesRespModel();
				quesRespModel.quesId=es.getId();
				quesRespModel.quesBody=es.getQuesBody();
				danxuan.add(quesRespModel);
			}else if (StringUtils.equals("8", quesType)){
			// 多选题
				QuesRespModel quesRespModel=new QuesRespModel();
				quesRespModel.quesId=es.getId();
				quesRespModel.quesBody=es.getQuesBody();
				duoxuan.add(quesRespModel);
			}else if (StringUtils.equals("2", quesType)){
			// 填空题
				QuesRespModel quesRespModel=new QuesRespModel();
				quesRespModel.quesId=es.getId();
				quesRespModel.quesBody=es.getQuesBody();
				tiankong.add(quesRespModel);
			}else {
			// 解答题
				QuesRespModel quesRespModel=new QuesRespModel();
				quesRespModel.quesId=es.getId();
				quesRespModel.quesBody=es.getQuesBody();
				jieda.add(quesRespModel);
			}
		}
		RespModel model=new RespModel();
		//单选题
		model.danxuan=danxuan;
		//多选题
		model.duoxuan=duoxuan;
		//填空题
		model.tiankong=tiankong;
		//解答题
		model.jieda=jieda;
		
		//知识点的数量
		model.knowCount=knowIds.size();
		return model;
	}


	protected static class ReqModel extends YxyApiReqModel {
		//试题的id和所有的知识点
		public List<QuesModel> list;
	}
	
	protected static class QuesModel {
		//试题的id
		public String quesId;
		//所有的知识点id
		public List<Integer> know;
		
	}
	protected static class RespModel extends YxyApiRespModel {
		//知识点的数量
		public Integer knowCount;
		//单选题
		public List<QuesRespModel> danxuan;
		//多选题
		public List<QuesRespModel> duoxuan;
		//填空题
		public List<QuesRespModel> tiankong;
		//解答题
		public List<QuesRespModel> jieda;
	}
	
	protected static class QuesRespModel {
		//试题id
		public String quesId;
		//试题的内容(比如题干等等)
		public Object quesBody;
	}
	
}
